package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.HashMap;
import java.util.List;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.erp.I_OrderProcessConfig;
import cn.ekuma.util.JsonUtil;

import com.openbravo.bean.erp.OrderProcessConfig;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.SerializerReadBasic;
import com.openbravo.data.loader.SerializerReadString;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class OrderProcessConfigDAO extends ModifiedLogDAO<OrderProcessConfig> {

	public OrderProcessConfigDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "ORDER_PROCESS_CONFIG",
                new Field[]{
                     new Field(I_OrderProcessConfig.ID,Datas.STRING,Formats.STRING),
                     new Field(I_OrderProcessConfig.ORDERTYPEID,Datas.STRING,Formats.STRING,OrderType.class),
                     new Field(I_OrderProcessConfig.PRIORITY,Datas.INT,Formats.INT),
                     new Field(I_OrderProcessConfig.PROCESSNAME,Datas.STRING,Formats.STRING),
                     new Field(I_OrderProcessConfig.PROP,Datas.STRING,Formats.STRING),
                     new Field(I_OrderProcessConfig.LASTMODIFIED,Datas.TIMESTAMP,Formats.TIMESTAMP)
                    },
                new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, OrderProcessConfig obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getOrderTypeId());
		dp.setInt(3,obj.getPriority());
		dp.setString(4,obj.getProcessName());
		dp.setString(5,JsonUtil.toString(obj.getProp()));
		dp.setTimestamp(6,obj.getLastModified());
	}

	@Override
	public Class getSuportClass() {
		return OrderProcessConfig.class;
	}

	@Override
	public OrderProcessConfig readValues(DataRead dr, OrderProcessConfig obj)
			throws BasicException {
		if(obj==null)
			obj=new OrderProcessConfig();
		  obj.setId(dr.getString(1));
		  obj.setOrderTypeId(dr.getString(2));
		  obj.setPriority(dr.getInt(3));
		  obj.setProcessName(dr.getString(4));
		  obj.setProp(JsonUtil.getHashMap(dr.getString(5)));
		  obj.setLastModified(dr.getTimestamp(6));
		return obj;
	}
	
	public HashMap findOrderProcessPropByNameAndOrderType(final String orderTypeId,
			final String processName) throws BasicException {
		String ret= (String) new StaticSentence(s
                , "SELECT PROP FROM ORDER_PROCESS_CONFIG WHERE ORDERTYPEID=? AND PROCESSNAME=?"
               ,SerializerWriteParams.INSTANCE, new SerializerReadBasic(new Datas[] {Datas.SERIALIZABLE}) ).find(new DataParams() {
                    public void writeValues() throws BasicException {
                    	setString(1, orderTypeId);
                    	setString(2,processName);
                    }});
		return JsonUtil.getHashMap(ret);
	}

	public List<String> findOrderSuperedRuleByOrderType(String orderTypeId) throws BasicException {
		return new StaticSentence(s, 
				"SELECT PROCESSNAME FROM ORDER_PROCESS_CONFIG WHERE ORDERTYPEID=?  ORDER BY PRIORITY"
				,SerializerWriteString.INSTANCE,SerializerReadString.INSTANCE ).list(orderTypeId);
	}
	
}
